//
//  XJCommunityServeTypeViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/31.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  社区---服务类比

#import "XJCommunityServeTypeViewController.h"

#import "XJServeTypeModel.h"

@interface XJCommunityServeTypeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableView;
/**数据源*/
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation XJCommunityServeTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"服务类别";
    
    [self.view addSubview:self.tableView];
    
    [self requestData];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight-XJTopNavH) style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (void)requestData {
    
    [XJHttpTool requestCommunityServeCategorySuccessH:^(id responseObjc) {
        XJLog(@"成功:%@",responseObjc);
        
        NSNumber *code = responseObjc[@"code"];
        if ([code isEqualToNumber:@0]) {
          NSDictionary *service_categoryDic =  responseObjc[@"data"][@"service_category"];
            for (NSDictionary *dic in service_categoryDic) {
                XJServeTypeModel *serveModel = [XJServeTypeModel mj_objectWithKeyValues:dic];
                
                [self.dataArray addObject:serveModel];
            }
        }
        
        //
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
       
    } failH:^(NSError *error) {
        [MBProgressHUD showError:[error localizedDescription]];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellId"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"UITableViewCellId"];
    }
    
    XJServeTypeModel *model = self.dataArray[indexPath.row];
    
    cell.textLabel.text = model.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XJServeTypeModel *model = self.dataArray[indexPath.row];
    
    XJLog(@"选择的服务类型是:====>%@",model.name);
    
    if (self.selectServeTypeBlock) {
        
        self.selectServeTypeBlock(model);
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
